'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:AdministratorChooseinfoCtrl
 * @description
 * # AdministratorChooseinfoCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('AdministratorChooseinfoCtrl', function ($scope, $rootScope, $window, $routeParams, $http, Util, openApi, localStorageService, PagingService) {
        var param = {};
        var filterId = "pkId";
        $scope.selectLists = []; //已经选择了的数组，用来回传
        // -----------------底部款项类型选框添加end-----------------------
        $scope.isSelect = function (index) {
            return $scope.isSelectindex == index
        }
        $scope.choosePeople = function (index) {
            $scope.searchInput = "";
            var curItemIndex;
            var curItem;
            curItem = $scope.taskList.items[index]
            curItemIndex = $scope.checkIfExsit(index);
            if (curItemIndex == -1) {
                $scope.selectLists.push(curItem)
            } else {
                $scope.selectLists.splice(curItemIndex, 1)
            }
            return $scope.selectLists;
        };
        $scope.focusInput = function () {
            $scope.selectLists.length = 0;
        };
        $scope.checkIfExsit = function (index) {
            var selecrIndex = -1;
            var filter = $scope.taskList.items[index][filterId];
            $scope.selectLists.forEach(function (item, index) {
                if (item[filterId] == filter) {
                    selecrIndex = index;
                    return selecrIndex;
                }
            });
            return selecrIndex;
        };
        // 搜索
        $scope.search = function () {
            var relatedModule;
            relatedModule = getParamUrl(); //获取借印章或者还印章
            // 出场申请接口不一样，参数也不一样，过滤条件也不一样
            if (relatedModule == "certificateType") {
                var porlist = {
                    "人员姓名": {
                        "name": "ownerName"
                    },
                    "证件类别": {
                        "name": "certificateType"
                    }
                }
                param.ownerName = $scope.searchInput || ""
                var config = {}; config.porlist = porlist; config.searchName = "姓名"
                $scope.taskList = new PagingService(openApi.attendanceCer, param, config);
            } else {
                param.filterSql = $scope.searchInput || "";
                param.relatedModule = relatedModule || "";
                $scope.taskList = new PagingService(openApi.getRelpage, param);
            }
        };
        // 根据不同的id请求的列表地址会不同
        var getParamUrl = function () {
            var url, form_id;
            form_id = $routeParams.id;
            if (form_id == "jyz") {
                // 借印章
                url = "sealBorrow_select_seal";
            } else if (form_id == "hyz") {
                // 还印章
                url = "sealReturn_select_seal";
            } else if (form_id == "ccsq") {
                //  出场申请的话过滤id不一样
                filterId = "cerId";
                url = "certificateType";
            }
            return url;
        };
        // 获取数据返回
        $scope.submit = function () {
            // 缓存中的数据
            var localSelectLists;
            if ($scope.selectLists.length < 1) {
                Util.Toast("请选择");
                return;
            }
            // localSelectLists = Util.getSelectLists();
            // $scope.selectLists = Util.mergeArr($scope.selectLists, localSelectLists, "pkId")
            localStorageService.set("selectLists", $scope.selectLists);
            $window.history.back();
        };
        // 获取类别

        $scope.getCerTypeList = function () {
            var param = {};
            param.type = '印章类别';
            Util.getcerTypeList(param, function (data) {
                cerkeyObj.tabList = data.data;
                $scope.showSheet('cerkeyObj');
            });
        }

        // 页面初始化请求数据
        var init = function () {
            $scope.search();
            $scope.selectLists = Util.getSelectLists();
        };
        init();
    });
